package me.kafeitu.demo.activiti;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.flowable.common.engine.impl.persistence.StrongUuidGenerator;
import org.flowable.common.rest.resolver.DefaultContentTypeResolver;
import org.flowable.dmn.api.DmnDecisionTable;
import org.flowable.dmn.api.DmnDeployment;
import org.flowable.dmn.api.DmnRepositoryService;
import org.flowable.dmn.api.DmnRuleService;
import org.flowable.dmn.engine.*;

import cn.tanghom.app.system.model.User;


public class FirstDmn {
	public static void main(String[] args) {

		// 根据默认配置创建引擎的配置实例
		DefaultContentTypeResolver res = null;
		StrongUuidGenerator uuid = new StrongUuidGenerator();
		
		
		DmnEngineConfiguration config = DmnEngineConfiguration.createDmnEngineConfigurationFromResourceDefault();

		// 创建规则引擎

		//-DmnEngine engine = config.buildDmnEngine();
		
		DmnEngine engine = DmnEngines.getDefaultDmnEngine();
	    DataSource dataSource = engine.getDmnEngineConfiguration().getDataSource();

		// 获取规则的存储服务组件

		DmnRepositoryService rService = engine.getDmnRepositoryService();

		// 获取规则服务组件

		DmnRuleService ruleService = engine.getDmnRuleService();

		// 进行规则 部署

		DmnDeployment dep = rService.createDeployment().addClasspathResource("dmn/first.dmn").deploy();

		// 进行数据查询

		DmnDecisionTable dt = rService.createDecisionTableQuery().deploymentId(dep.getId()).singleResult();

		// 初始化参数

		Map<String, Object> params = new HashMap<String, Object>();

		params.put("personAge", 19);

		
		
		// 传入参数执行决策，并返回结果

		Map<String, Object> result = ruleService.createExecuteDecisionBuilder().decisionKey(dt.getKey()).variables(params).executeWithSingleResult();

		// 控制台输出结果

		System.out.println(result.get("myResult"));

		// 重新设置参数

		params.put("personAge", 5);

		// 重新执行决策

		Map<String, Object> result2 = ruleService.createExecuteDecisionBuilder().decisionKey(dt.getKey()).variables(params).executeWithSingleResult();

		// 控制台重新输出结果

		System.out.println(result2.get("myResult"));

	}
}
